define(['./../initial'], function() {
    var app = angular.module('App.home');
    app.factory('toggleSideBar', ['$q', '$timeout', '$rootScope', 'prjStatusChartModalHelper',
        function($q, $timeout, $rootScope, prjStatusChartModalHelper) {

            var eventVar = null;

            function dispachResize() {
                var evt = eventVar || document.createEvent('UIEvents');
                evt.initUIEvent('resize', true, false, window, 0);
                window.dispatchEvent(evt);
            }

            var obj = {
                dispachResize: dispachResize,
                toggleLeftSideBar: function(action) {
                    var defer = $q.defer();
                    var leftBar = $('#leftBar');
                    var bottomBar = $('#bottomBar');
                    prjStatusChartModalHelper.close();
                    if (bottomBar.hasClass('show')) {
                        bottomBar.addClass('hide').removeClass('show');
                    }

                    if (leftBar.sidebar('is open') && action === 'show') {
                        //do nothing
                    } else if (!leftBar.sidebar('is open') && action === 'hide') {
                        //do nothing
                    } else {
                        leftBar.sidebar({
                            overlay: $rootScope.smallScreen ? true : false
                        }).sidebar(action ? action : 'toggle');
                    }
                    $timeout(function() {
                        dispachResize();
                        defer.resolve(null);
                    }, 400);
                    return defer.promise;
                },
                toggleBottomSideBar: function(action) {
                    var defer = $q.defer();
                    var leftBar = $('#leftBar');
                    var bottomBar = $('#bottomBar');

                    prjStatusChartModalHelper.close();
                    
                    if (!action) {
                        if (bottomBar.hasClass('show')) {
                            action = "hide";
                        } else {
                            action = "show";
                        }
                    } else {

                        if (action === 'hide' && (bottomBar.hasClass('hide') || !bottomBar.hasClass('show'))) {
                            defer.resolve(null);
                            return defer.promise;
                        } else if (action === 'show' && bottomBar.hasClass('show')) {
                            defer.resolve(null);
                            return defer.promise;
                        }
                    }
                    var preAction = action === 'show' ? 'hide' : 'show';
                    if (action === 'show' && $rootScope.smallScreen) {
                        obj.toggleLeftSideBar('hide').then(function() {
                            bottomBar.removeClass(preAction).addClass(action);
                            $timeout(function() {
                                defer.resolve(null);
                            }, 400);
                        });
                    } else {
                        bottomBar.removeClass(preAction).addClass(action);
                        $timeout(function() {
                            defer.resolve(null);
                        }, 400);
                    }
                    return defer.promise;
                }
            };
            return obj;
        }

    ]);
});
